﻿using System.Web.Mvc;
using CMSMVC4.Filters;
using System.Linq;
using CMSMVC4.Areas.Admin.Models;

namespace CMSMVC4.Areas.Admin.Controllers
{
    [Authorize(Roles = "Admin,SupportOnline")]
    public class SupportOnlineController : Controller
    {
        readonly CMSMVC4Entities _db = new CMSMVC4Entities();
        //
        // GET: /Admin/SupportOnline/
        [HttpGet]
        public ActionResult Index()
        {
            ViewBag.M = new CommandSql().GetParrentByMenu("/Admin/SupportOnline").Rows[0]["Id"];
            var data = new CommandSql().GetAllSupport();
            return View(data);
        }

        //
        //GET: /Admin/SupportOnline/Add
        #region Add
        [HttpGet]
        public ActionResult Add()
        {
            ViewBag.M = new CommandSql().GetParrentByMenu("/Admin/SupportOnline/Add").Rows[0]["Parrent"];
            return View();
        }

        [HttpPost]
        public ActionResult Add(SupportOnlineModel model)
        {
            if (ModelState.IsValid)
            {
                var data = new Introduction {Url = model.Url, Type = model.Type,Name = model.Name};
                _db.Introductions.AddObject(data);
                _db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(model);
        }
        #endregion

        //
        //GET: /Admin/SupportOnline/Edit
        #region Edit
        [HttpGet]
        public ActionResult Edit(int id)
        {
            var data = _db.Introductions.FirstOrDefault(x => x.Id == id);
            if (data != null)
            {
                ViewBag.Name = data.Name;
                ViewBag.Support = data.Url;
            }
            return PartialView();
        }

        [HttpPost]
        public ActionResult Edit(int id, SupportOnlineModel model)
        {
            if (ModelState.IsValid)
            {
                var data = _db.Introductions.FirstOrDefault(x => x.Id == id);
                if (data != null)
                {
                    data.Name = model.Name;
                    data.Url = model.Url;
                }
                _db.SaveChanges();
            }
            if (Request.IsAjaxRequest())
            {
                return JavaScript("window.location='/Admin/SupportOnline/Index'");
            }
            return RedirectToAction("Index");
        }
        #endregion

        //
        //GET: /Admin/SupportOnline/Delete
        #region Delete
        [HttpGet]
        public ActionResult Delete(int id)
        {
            return PartialView();
        }

        [HttpPost]
        public ActionResult Delete(string[] id)
        {
            if (id != null)
            {
                foreach (var i in id)
                {
                    int idcomment;
                    int.TryParse(i, out idcomment);
                    var row = _db.Introductions.FirstOrDefault(x => x.Id == idcomment);
                    _db.Introductions.DeleteObject(row);
                    _db.SaveChanges();
                }
            }
            return Json(string.Empty, JsonRequestBehavior.AllowGet);
        }

        #endregion
    }
}
